
'use strict';

angular.module('myApp.check', ['angularFileUpload'])

.controller('checkCtrl', ['$scope','$http', '$upload',
    function($scope,$http,$upload) {
    $http.get('v1/attendance').success(function(data){
        $scope.checks = data.result;
       // console.log(data);
    });

    $scope.history = function(){
         var month = {'month':$('#month').val()}
         console.log(month);
        $http.post('v1/attendance/search',month).success(function(data){
            if( !data.error ){
                alert("新增成功","success");
                $scope.checks = data.result;
            }else{
                alert(data.message);
            }
        });
    };
    
    $scope.$watch('files', function () {
        $scope.upload($scope.files);
    });
    $scope.upload = function (files) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                $upload.upload({
                    url: 'v1/student_upload_file',
                    fields: {'id': '123'},
                    file: file,
                    data: file,
                    method: 'POST',
                    fileFormDataName:"myfile",
                    headers: {'Content-Type': file.type}
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
                }).success(function (data, status, headers, config) {
                	console.log(data);
                    console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
                });
            }
        }
    };

    $('.year').bootstrapMonthpicker();
}]);